/* aliases */
let Application = PIXI.Application,
	loader = PIXI.loader
;

/* create pixi application */
let app = new Application();
document.body.appendChild(app.view);

/* capture input */
app.view.onmousedown = mouse_input;
//app.view.onmouseup = mouse_input;

/* save game dimensions */
let width = app.renderer.view.width;
let height = app.renderer.view.height;

/* mute */
let muted = false;

/* initialize world */
let world = new world_list[0]();
let input = [];
let interval;

/* load assets from world */
loader
	.add( world.assets() )
	.load( loop );

/* start game */
function loop() {
	/* initialize world */
	world.start();

	/* main loop */
	interval = setInterval( update, 1000/30 );
}

/* runs once per frame */
function update() {
	/* update world and save result 
	 * a return value of 0 or higher is the index of a new world
	 * that is meant to open
	 */
	let i = world.update();
	if (i >= 0) {

		/* remove unprocessed input */
		input.splice(0);

		/* remove all elements from application */
		app.stage.removeChildren();

		/* create new world */
		world = new world_list[i](world.returner);

		/* stop previous loop */
		clearInterval(interval);

		/* reset loader to load new assets */
		loader.reset();

		/* load new world's assets and begin new loop */
		loader.add( world.assets() )
			.load( loop );

		/* stop running this loop */
		return;
	}

	/* i usually use this, but so far turns out PIXI doesn't need this
	 * i might remove it once i confirm i won't be using it in the future
	 */
	world.draw();

	/* remove unprocessed input */
	input.splice(0);
} /* update */

/* for now, every time accepted input is recorded,
 * save it in an array, and process it in the game
 */
function mouse_input(e) {
	input.push(e);
}
